const baseURL = "http:localhost:9000"

function login(data) {
    console.log(data)
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/user/login",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            success: function (e) {
                // console.log(e)
                if (e.flag) {
                    //localStorage 可以跨同浏览器窗口
                    localStorage.token = e.data.token;
                    localStorage.userName = e.data.user.userName;
                    localStorage.userId = e.data.user.userId;
                    localStorage.identity = e.data.user.identity;
                    localStorage.state =  e.data.user.state
                    // console.log(localStorage.token)
                    // console.log(localStorage.userName)
                    // console.log(localStorage.userId)
                    // console.log(localStorage.identity)
                    // console.log(localStorage.state)
                    // 谁调用，就是从谁开始相对路径，而不是该request.js相对路径
                    if (localStorage.identity == 1) {
                        window.location.href="./backindex.html"; 
                    } else {
                        window.location.href="./userindex.html"; 
                    }
               
                   
                } else {
                     alert("账号或密码错误")
                }
            },
            error: function (message) {
                alert("账号或密码错误")
            }
        });
    });	
}


function getUserList(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/user/query",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}

function addUser(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/user/add",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}

function getApprovalList(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/resume-information/query",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}


function getLogList(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/log/query",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}

function getTodoList(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/todo/query",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}


function getStatisticsList(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/statistics/query",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}



function updateResume(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/resume/update",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}

function getResume(data, callback) {
    // 依靠调入端自动传递函数格式
    $(function() {
        $.ajax({
            type: "POST",
            url: baseURL+"/resume/query",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            dataType: "json",
            headers:{'Authorization': "CUIT "+localStorage.token},
            success: callback,
            error: function (message) {
                console.log(message)
            }
        });
    });	
}

function uploadImage(data, callback) {
    //判断是否有选择上传文件
    var imgPath = $("#uploadFile").val();
    if (imgPath == "") {
        alert("请选择上传图片！");
        return;
    }
    //判断上传文件的后缀名
    var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
    if (strExtension != 'jpg' && strExtension != 'gif'
    && strExtension != 'png' && strExtension != 'bmp') {
        alert("请选择图片文件");
        return;
    }
    $(function() {
        $.ajax({
            type: "POST",
            url: "handler/UploadImageHandler.ashx",
            data: { imgPath: $("#uploadFile").val() },
            cache: false,
            success: function(data) {
                alert("上传成功");
                $("#imgDiv").empty();
                $("#imgDiv").html(data);
                $("#imgDiv").show();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("上传失败，请检查网络后重试");
            }
        });
    });
}


